﻿using System.Data.Entity;
using Framework.Castle.Windsor;
using Framework.Data.Implement;
using System.Data.SqlClient;
using System;
using System.Configuration;
using Framework.Common;

namespace Framework.Data
{
    /// <summary>
    /// Repository 工厂类
    /// </summary>
    public static class RepositoryFactory
    {
        /// <summary>
        /// 使用自定义的DbContext
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public static IRepository GetRepository(this DbContext db)
        {
            return new GenericRepository(db);
        }
        /// <summary>
        /// 调用默认注册的DbContext
        /// </summary>
        /// <returns></returns>
        public static IRepository GetRepository()
        {
            var dbContext = ContainerHelper.Resolve<DbContext>();
            return new GenericRepository(dbContext);
        }
        /// <summary>
        /// 根据名称调用注册的DbContext
        /// </summary>
        /// <param name="resolveKey"></param>
        /// <returns></returns>
        public static IRepository GetRepository(string resolveKey)
        {
            var dbContext = ContainerHelper.Resolve<DbContext>(resolveKey);
            return new GenericRepository(dbContext);
        }

        /// <summary>
        /// 获取简易的数据操作对象（plain sql）
        /// </summary>
        /// <param name="dbConfig"></param>
        /// <returns></returns>
        public static IRepository GetSimpleRepository(string dbConfig)
        {
            return new GenericContext(dbConfig).GetRepository();
        }

        /// <summary>
        /// 
        /// </summary>
        private class GenericContext : DbContext
        {
            public GenericContext(string dbConfig)
                : base("name=" + dbConfig)
            { }
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="dbConfig"></param>
        /// <param name="table"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static bool BatchInsert(string dbConfig, System.Data.DataTable table, string tableName)
        {
            try
            {
                string connectionString = ConfigurationManager.ConnectionStrings[dbConfig].ConnectionString;

                if (table != null && table.Rows.Count != 0)
                {
                    using (var sqlConnection = new SqlConnection(connectionString))
                    {
                        sqlConnection.Open();
                        using (var sqlBulkCopy = new SqlBulkCopy(connectionString))
                        {
                            sqlBulkCopy.DestinationTableName = tableName;
                            sqlBulkCopy.BatchSize = table.Rows.Count;
                            sqlBulkCopy.WriteToServer(table);
                            sqlBulkCopy.Close();
                        }
                        sqlConnection.Close();
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Fatal("批量插入错误", ex);
                return false;
            }
        }
    }
}
